package com.lyn.solitude.host_ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.lyn.solitude.entity.Plugin
import com.lyn.solitude.host_ui.adapter.PluginAdapter.PluginViewHolder
import com.lyn.solitude.host_ui.databinding.AdapterPluginEmptyItemBinding
import com.lyn.solitude.host_ui.databinding.AdapterPluginItemBinding
import com.lyn.solitude.pluginhost.PluginManager

/**
 * 插件适配器
 *
 * @author longyn 2024/4/20
 * @version 1.0.0
 */
class PluginAdapter : RecyclerView.Adapter<PluginViewHolder>() {
    private var emptyPluginListener: EmptyPluginListener? = null
    private val list: MutableList<Plugin> = ArrayList()
    private var onItemClickListener: OnItemClickListener? = null
    private var onItemLongClickListener: OnItemLongClickListener? = null
    @SuppressLint("NotifyDataSetChanged")
    fun setPluginData(list: List<Plugin>?) {
        this.list.clear()
        if (list == null) {
            notifyDataSetChanged()
            return
        }
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    fun remove(position: Int) {
        list.removeAt(position)
        notifyItemRemoved(position)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        this.onItemClickListener = onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener?) {
        this.onItemLongClickListener = onItemLongClickListener
    }

    fun setEmptyPluginListener(emptyPluginListener: EmptyPluginListener) {
        this.emptyPluginListener = emptyPluginListener
    }

    @SuppressLint("NotifyDataSetChanged")
    fun clear() {
        list.clear()
        notifyDataSetChanged()
    }

    fun getItem(position: Int): Plugin? {
        return if (list.isEmpty()) {
            null
        } else list[position]
    }

    override fun getItemViewType(position: Int): Int {
        return if (list.isEmpty()) {
            -1
        } else super.getItemViewType(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PluginViewHolder {
        if (viewType == -1) {
            val binding = AdapterPluginEmptyItemBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
            val layoutParams = binding.getRoot().layoutParams
            layoutParams.width = -1
            layoutParams.height = -1
            return PluginViewHolder(binding)
        }
        val binding = AdapterPluginItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        val layoutParams = binding.getRoot().layoutParams
        layoutParams.width = parent.width / 4
        layoutParams.height = -2
        return PluginViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PluginViewHolder, position: Int) {
        if (list.isEmpty()) {
            return
        }
        val plugin = list[position]
        if (holder.binding is AdapterPluginItemBinding) {
            val binding = holder.binding as AdapterPluginItemBinding
            binding.name.text = plugin.name
            val bitmap = PluginManager.getLogo(plugin.id)
            binding.logoImage.setImageBitmap(bitmap)
            holder.binding.root.setOnClickListener {
                if (onItemClickListener != null) {
                    onItemClickListener!!.onItemClick(holder, position)
                }
            }
            holder.binding.root.setOnLongClickListener {
                if (onItemLongClickListener != null) {
                    return@setOnLongClickListener onItemLongClickListener!!.onItemLongClick(
                        holder,
                        position
                    )
                }
                false
            }
        }
    }

    override fun getItemCount(): Int {
        if (list.isEmpty()) {
            if (emptyPluginListener != null) {
                emptyPluginListener!!.onEmpty()
            }
            return 1
        }
        if (emptyPluginListener != null) {
            emptyPluginListener!!.onNotEmpty()
        }
        return list.size
    }

    class PluginViewHolder(var binding: ViewBinding) : RecyclerView.ViewHolder(
        binding.root
    )

    interface EmptyPluginListener {
        fun onEmpty()
        fun onNotEmpty()
    }

    interface OnItemClickListener {
        fun onItemClick(holder: PluginViewHolder?, position: Int)
    }

    interface OnItemLongClickListener {
        fun onItemLongClick(holder: PluginViewHolder?, position: Int): Boolean
    }
}
